package net.csdn.business.discuss.api.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.csdn.business.discuss.api.mapper.DiscussVoteMapper;
import net.csdn.business.discuss.api.mapper.DiscussVoteOptionMapper;
import net.csdn.business.discuss.api.mapper.DiscussVoteRecordMapper;
import net.csdn.business.discuss.api.model.dto.NewDiscussVoteRecordDTO;
import net.csdn.business.discuss.api.model.dto.UpdateDiscussVoteRecordDTO;
import net.csdn.business.discuss.api.model.entity.DiscussVoteRecord;
import net.csdn.business.discuss.api.model.query.DiscussVoteRecordQuery;
import net.csdn.business.discuss.api.model.vo.DiscussVoteRecordVO;
import net.csdn.business.discuss.api.service.IDiscussVoteRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 讨论投票记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-25
 */
@Service
@RequiredArgsConstructor
public class DiscussVoteRecordServiceImpl extends ServiceImpl<DiscussVoteRecordMapper, DiscussVoteRecord> implements IDiscussVoteRecordService
{

    private final DiscussVoteMapper discussVoteMapper;

    private final DiscussVoteOptionMapper discussVoteOptionMapper;

    /**
     * 分页查询讨论投票记录列表
     *
     * @param query 讨论投票记录
     * @return 讨论投票记录
     */
    @Override
    public IPage<DiscussVoteRecordVO> findPageList(DiscussVoteRecordQuery query) {
        //获得Page对象
        Page<DiscussVoteRecordVO> page = query.getPage();
        List<DiscussVoteRecordVO> pageList=baseMapper.findPageList(page,query);
        page.setRecords(pageList);
        return page;
    }

    /**
     * 根据id查询讨论投票记录
     * 
     * @param query 讨论投票记录主键
     * @return 讨论投票记录
     */
    @Override
    public DiscussVoteRecordVO selectDiscussVoteRecord(DiscussVoteRecordQuery query)
    {
        return baseMapper.selectDiscussVoteRecord(query);
    }

    /**
     * 查询讨论投票记录列表
     * 
     * @param query 讨论投票记录
     * @return 讨论投票记录
     */
    @Override
    public List<DiscussVoteRecordVO> selectDiscussVoteRecordList(DiscussVoteRecordQuery query)
    {
        return baseMapper.selectDiscussVoteRecordList(query);
    }

    /**
     * 新增讨论投票记录
     *
     * @param params 讨论投票记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertDiscussVoteRecord(NewDiscussVoteRecordDTO params)
    {
        DiscussVoteRecord discussVoteRecord=new DiscussVoteRecord();
        BeanUtils.copyProperties(params,discussVoteRecord);
        String id= IdUtil.fastSimpleUUID();
        discussVoteRecord.setObjectId(id);
        discussVoteRecord.setCreatedBy(params.getUserId());
        int result=baseMapper.insert(discussVoteRecord);
        if(result>0){
            discussVoteMapper.addVoteTotal(params.getDiscussId());
            discussVoteOptionMapper.addVoteTotal(params.getOptionId(),1);
        }
        return result;
    }

    /**
     * 修改讨论投票记录
     *
     * @param params 讨论投票记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateDiscussVoteRecord(UpdateDiscussVoteRecordDTO params)
    {
        //修改投票选项
        LambdaUpdateWrapper<DiscussVoteRecord> updateWrapper = new UpdateWrapper<DiscussVoteRecord>().lambda();
        updateWrapper.eq(DiscussVoteRecord::getDiscussId, params.getDiscussId());
        updateWrapper.eq(DiscussVoteRecord::getVoteId,params.getVoteId());
        updateWrapper.eq(DiscussVoteRecord::getUserId,params.getUserId());
        DiscussVoteRecord discussVoteRecord=new DiscussVoteRecord();
        discussVoteRecord.setOptionId(params.getOptionId());
        discussVoteRecord.setLastModifiedBy(params.getUserId());
        int result=baseMapper.update(discussVoteRecord,updateWrapper);
        if(result>0){
            String oldOptionId=params.getOldOptionId();
            String optionId=params.getOptionId();
            discussVoteOptionMapper.addVoteTotal(oldOptionId,-1);
            discussVoteOptionMapper.addVoteTotal(optionId,1);
        }
        return result;
    }

    @Override
    public Boolean selectIsVote(DiscussVoteRecordQuery query) {
        DiscussVoteRecordVO recordVO=baseMapper.selectDiscussVoteRecord(query);
        if(recordVO==null){
            return false;
        }
        return true;
    }
}
